﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Philip Stears 
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@connectbasic.net.
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

namespace ConnectBasic.Shell.Hosting.OpenXmlFileProjectStoreInternals
{
    using System.Diagnostics;
    using System.Xml.Linq;

    internal class ProjectStoreFolder : IProjectStoreFolder
    {
        private readonly static XNamespace NS = OpenXmlFileProjectStore.NS;

        private readonly OpenXmlFileProjectStore mStore;
        private readonly XElement mFolderElement;
        private readonly XAttribute mNameAttribute;

        private readonly ProjectStoreFolderCollection mFolders;
        private readonly ProjectStoreItemCollection mItems;

        #region .ctor

        internal ProjectStoreFolder(OpenXmlFileProjectStore store, XElement folderElement)
        {

            // Assert parameters
            Debug.Assert(store != null);
            Debug.Assert(folderElement != null);
            Debug.Assert(folderElement.Name == NS + "Folder");

            mStore = store;
            mFolderElement = folderElement;

            mNameAttribute = folderElement.Attribute("Name");
            Debug.Assert(mNameAttribute != null);

            // Load folders and items
            var foldersElement = folderElement.Element(NS + "Folders");
            Debug.Assert(foldersElement != null);
            mFolders = new ProjectStoreFolderCollection(store, foldersElement);

            var itemsElement = folderElement.Element(NS + "Items");
            Debug.Assert(itemsElement != null);
            mItems = new ProjectStoreItemCollection(store, itemsElement);

        }

        #endregion

        #region IProjectStoreFolder Members

        public string Name
        {
            get { return mNameAttribute.Value; }
        }

        public IProjectStoreFolderCollection Folders
        {
            get { return mFolders; }
        }

        public IProjectStoreItemCollection Items
        {
            get { return mItems; }
        }

        #endregion
    }
}
